import { http } from "@/utils/http";
import { baseUrlApi } from "./utils";

export type UserResult = {
  code: boolean;
  data: {
    /** 头像 */
    avatar: string;
    /** 用户名 */
    username: string;
    /** 昵称 */
    nickname: string;
    /** 当前登录用户的角色 */
    roles: Array<string>;
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
  msg: string;
};

export type RefreshTokenResult = {
  code: boolean;
  data: {
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};

/** 登录 */
export const getLogin = (data?: object) => {
  return http.request<UserResult>(
    "post",
    baseUrlApi("/admin/system/admin/login"),
    {
      data
    },
    {
      withCredentials: true
    }
  );
};

/** 刷新`token` */
export const refreshTokenApi = (data?: object) => {
  return http.request<RefreshTokenResult>(
    "post",
    baseUrlApi("/admin/system/admin/refresh_token"),
    {
      data
    }
  );
};

/** 获取登录验证码 */
export const getCaptcha = (data?: object) => {
  return http.request<any>(
    "get",
    baseUrlApi("/admin/system/admin/get_captcha"),
    { data },
    { responseType: "blob" }
  );
};

/** 获取代办事项 */
export const getTodoList = (data?: object) => {
  return http.request<any>("get", baseUrlApi("/admin/system/admin/todo"), {
    data
  });
};
